<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Http\Controllers\Admin\System;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\System\Permission;

class PermissionController extends Controller
{
    public function __construct()
    {
        $select = [ 'id', 'name', 'parent_id', 'label', 'icon', 'sort_order', 'pid','created_at' ];
        view()->share([
            'permissions' => Permission::all_permission($select),//所有权限列表
        ]);
    }


    /**
     * 权限列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('system.permission.index');
    }

    /**
     * 权限新增列表
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create( Request $request )
    {
        return view('system.permission.create', compact('request'));
    }

    /**
     * 权限添加
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store( Request $request )
    {
        return $this->getPermission()->PermissionStoreService($request);
    }

    /**
     * 权限编辑页面
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit( $id )
    {
        return $this->getPermission()->PermissionEditService($id);
    }

    /**
     * 权限修改
     *
     * @param Request $request
     * @param         $id
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update( Request $request, $id )
    {
        request()->offsetSet('id', $id);
        return $this->getPermission()->PermissionUpdateService($request);
    }

    /**
     * 删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy( $id )
    {
        return $this->getPermission()->PermissionDestroyService($id);
    }

    /**
     * 排序
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function sort_order( Request $request )
    {
        return $this->getPermission()->PermissionOrderService($request);
    }
}
